package windows;

import beans.SenSorReading;
import org.apache.commons.collections.IteratorUtils;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class TimeWindowTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> inputStream = env.readTextFile("src/main/resources/sensor.txt");

        SingleOutputStreamOperator<SenSorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SenSorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });
        //开窗测试
        /**
         * 传一个参数就是滚动
         * 传两个参数就是滑动
         * 增量聚合
         */
        SingleOutputStreamOperator<Integer> aggregate = dataStream
                .keyBy("id")
                .timeWindow(Time.seconds(15))
                /**
                 * 三个参数分别是输入、累加器和输出
                 */
                .aggregate(new AggregateFunction<SenSorReading, Integer, Integer>() {
                    @Override
                    public Integer createAccumulator() {
                        return 0;
                    }

                    /**\
                     * 在什么时候启用累加，如何累加
                     * @param senSorReading
                     * @param integer
                     * @return
                     */
                    @Override
                    public Integer add(SenSorReading senSorReading, Integer integer) {
                        return integer + 1;
                    }

                    @Override
                    public Integer getResult(Integer integer) {
                        return integer;
                    }

                    /**
                     * 合并多个分区的数据
                     * @param integer
                     * @param acc1
                     * @return
                     */
                    @Override
                    public Integer merge(Integer integer, Integer acc1) {
                        return integer + acc1;
                    }
                });

        /**
         * 全窗口函数
         */
        SingleOutputStreamOperator<Integer> apply = dataStream
                .keyBy("id")
                .timeWindow(Time.seconds(15))
                /**
                 * 四个参数：输入、输出、输入的key，和窗口
                 */
                .apply(new WindowFunction<SenSorReading, Integer, Tuple, TimeWindow>() {
                    @Override
                    public void apply(Tuple tuple, TimeWindow timeWindow, Iterable<SenSorReading> iterable, Collector<Integer> collector) throws Exception {
                        Integer count = IteratorUtils.toList(iterable.iterator()).size();
                        collector.collect(count);
                    }
                });

        /**
         * 这是超市数据已经hi单独的流，可以单独拿出来处理
         */
        aggregate.print();
        env.execute();
    }
}
